package cn.edu.neu.springbootdemo.action


import cn.edu.neu.springbootdemo.service.GoodsService
import org.apache.commons.io.FileUtils
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Controller
import org.springframework.ui.Model
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RequestMethod
import org.springframework.web.bind.annotation.ResponseBody
import org.springframework.web.multipart.MultipartFile
import java.io.File
import java.io.IOException


@Controller
@RequestMapping("/goods")
class GoodsAction {
    @Autowired(required = false)
    private lateinit var goodsService: GoodsService

    @RequestMapping("getGoods")
    fun getGoods(model: Model): String{
        val goodsList = goodsService.getGoods()
        model.addAttribute("goods",goodsList)
        return "/admin/shop"
    }

    @RequestMapping("getGood")
    fun getGood(model: Model): String{
        val goodsList = goodsService.getGoods()
        model.addAttribute("goods",goodsList)
        return "/user/shop"
    }

    @RequestMapping(value= ["delGoods"],method = arrayOf(RequestMethod.GET))
    @ResponseBody
    fun delGoods(id: Int?):Boolean{
        val path = System.getProperty("user.dir")+"\\src\\main\\resources\\static\\images\\$id"+".jpg"
        val file = File(path)
        if (file.exists() && file.isFile){
            file.delete()
            return goodsService.delGoods(id)
        }else{
            print("删除失败")
            return goodsService.delGoods(id)
        }


    }

    @RequestMapping(value = ["changeGoods"],method = arrayOf(RequestMethod.POST))
    @ResponseBody
    fun changeGoods(id: Int,name: String, price: String, remain: String, type: String):Boolean{
        return goodsService.changeGoods(id, name, price, remain, type)
    }

    @RequestMapping(value = ["addGoods"],method = arrayOf(RequestMethod.POST))
    @ResponseBody
    fun addGoods(name: String, price: String, file: MultipartFile, remain: String, type: String):Boolean{
        val id = name.hashCode()
        val path = System.getProperty("user.dir")+"\\src\\main\\resources\\static\\images\\"
        val filePath = File(path)
        if (!filePath.exists()){
            filePath.mkdirs()
        }
        if (file.isEmpty){
            return false
        }
        val suf = file.originalFilename.substring(file.originalFilename.indexOf(".")+1)
        val fileNameA = "$id.$suf"
        try {
            FileUtils.writeByteArrayToFile(File(path+fileNameA),file.bytes)
        }catch (e:IOException){
            e.printStackTrace()
            return false
        }
        val pic = "../images/"+fileNameA
        return goodsService.addGoods(id, name, price, pic, type, remain)
    }

    @RequestMapping(value = ["findGoods"],method = arrayOf(RequestMethod.POST))
    fun findGoods(name: String?,model: Model): String{
        val goodsList = goodsService.findGoods(name)
        model.addAttribute("goods",goodsList)
        return "/admin/shop"
    }
}